package com.user.servlet;

import com.user.entity.BookInfo;
import com.user.entity.Page;
import com.user.service.IUserService;
import com.user.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "QueryBookServlet", value = "/QueryBookServlet")
public class QueryBookServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String bookname = request.getParameter("bookname");
        String author = request.getParameter("author");
        String category = request.getParameter("category");
        int number = Integer.parseInt(request.getParameter("number"));
        String adminname = request.getParameter("adminname");
        IUserService userService = new UserServiceImpl();
        List<BookInfo> bookList = userService.queryBooksByCondition(new BookInfo(0, bookname, author, category, number));
        Page page = new Page();
        page.setBookList(bookList);

        request.setAttribute("p", page);


        if (adminname != null)
            request.getRequestDispatcher("Admin/queryBookResult.jsp").forward(request, response);
        request.getRequestDispatcher("User/queryFormResult.jsp").forward(request, response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
